Ext.define('Admin.view.main.MainController', {extend:Ext.app.ViewController, alias:'controller.main', listen:{controller:{'#':{unmatchedroute:'onRouteChange'}}}, routes:{':node':'onRouteChange'}, lastView:null, setCurrentView:function(hashTag) {
  hashTag = (hashTag || '').toLowerCase();
  var me = this, refs = me.getReferences(), mainCard = refs.mainCardPanel, mainLayout = mainCard.getLayout(), navigationList = refs.navigationTreeList, store = navigationList.getStore(), node = store.findNode('routeId', hashTag) || store.findNode('viewType', hashTag), view = node && node.get('viewType'), lastView = me.lastView, existingItem = mainCard.child('component[routeId\x3d' + hashTag + ']'), newView;
  if (lastView && lastView.isWindow) {
    lastView.destroy();
  }
  lastView = mainLayout.getActiveItem();
  if (!existingItem) {
    newView = Ext.create({xtype:view, routeId:hashTag, hideMode:'offsets'});
  }
  if (!newView || !newView.isWindow) {
    if (existingItem) {
      if (existingItem !== lastView) {
        mainLayout.setActiveItem(existingItem);
      }
      newView = existingItem;
    } else {
      Ext.suspendLayouts();
      mainLayout.setActiveItem(mainCard.add(newView));
      Ext.resumeLayouts(true);
    }
  }
  navigationList.setSelection(node);
  if (newView.isFocusable(true)) {
    newView.focus();
  }
  me.lastView = newView;
}, onNavigationTreeSelectionChange:function(tree, node) {
  var to = node && (node.get('routeId') || node.get('viewType'));
  if (to) {
    this.redirectTo(to);
  }
}, onToggleNavigationSize:function() {
  var me = this, refs = me.getReferences(), navigationList = refs.navigationTreeList, wrapContainer = refs.mainContainerWrap, collapsing = !navigationList.getMicro(), new_width = collapsing ? 64 : 250;
  if (Ext.isIE9m || !Ext.os.is.Desktop) {
    Ext.suspendLayouts();
    refs.senchaLogo.setWidth(new_width);
    navigationList.setWidth(new_width);
    navigationList.setMicro(collapsing);
    Ext.resumeLayouts();
    wrapContainer.layout.animatePolicy = wrapContainer.layout.animate = null;
    wrapContainer.updateLayout();
  } else {
    if (!collapsing) {
      navigationList.setMicro(false);
    }
    navigationList.canMeasure = false;
    refs.senchaLogo.animate({dynamic:true, to:{width:new_width}});
    navigationList.width = new_width;
    wrapContainer.updateLayout({isRoot:true});
    navigationList.el.addCls('nav-tree-animating');
    if (collapsing) {
      navigationList.on({afterlayoutanimation:function() {
        navigationList.setMicro(true);
        navigationList.el.removeCls('nav-tree-animating');
        navigationList.canMeasure = true;
      }, single:true});
    }
  }
}, onMainViewRender:function() {
  if (!window.location.hash) {
    this.redirectTo('dashboard');
  }
}, onRouteChange:function(id) {
  this.setCurrentView(id);
}, onSearchRouteChange:function() {
  this.setCurrentView('searchresults');
}, onSwitchToModern:function() {
  Ext.Msg.confirm('Switch to Modern', 'Are you sure you want to switch toolkits?', this.onSwitchToModernConfirmed, this);
}, onSwitchToModernConfirmed:function(choice) {
  if (choice === 'yes') {
    var s = window.location.search;
    s = s.replace(/(^\?|&)classic($|&)/, '').replace(/^\?/, '');
    window.location.search = ('?modern\x26' + s).replace(/&$/, '');
  }
}, logout:function() {
  var me = this;
  Ext.Ajax.request({url:'logout', method:'post', success:function(response, options) {
    var json = Ext.util.JSON.decode(response.responseText);
    if (json.success) {
      me.redirectTo('', true);
      window.location.reload();
    } else {
      Ext.Msg.alert('登出失败', json.msg);
    }
  }});
}, openResetWindow:function(view, recIndex, cellIndex, item, e) {
  var cfg = Ext.apply({xtype:'ResetWindow', items:[Ext.apply({xtype:'ResetForm'})]});
  var win = Ext.create(cfg);
}, submitResetForm:function(btn) {
  var password = Ext.getCmp('newPwd1').value;
  var repassword = Ext.getCmp('newPwd2').value;
  console.log(password);
  console.log(repassword);
  if (password != repassword) {
    window.alert('您输入的新密码与确认密码确认不一致');
    return false;
  } else {
    var form = btn.up('form');
    form.getForm().submit({url:'user/reset', params:{id:18, newPwd:password}, success:function(view, action) {
      Ext.Msg.alert('提示', action.result.msg, function() {
        btn.up('window').close();
      });
    }, failure:function(view, action) {
      Ext.Msg.alert('提示', action.result.msg);
    }});
  }
}, onEmailRouteChange:function() {
  this.setCurrentView('email');
},openMyPerWindow: function() {
          var cfg = Ext.apply({
                      xtype: 'myPerWindow',
                      items: [Ext.apply({xtype: 'myPerForm'})]
          });
          var win = Ext.create(cfg);
          var formItem1 = Ext.getCmp('roleperform3');
          var formItem2 = Ext.getCmp('roleperform4');
          Ext.Ajax.request({  
                url : 'role/findMyPer',  
                params : {id : loginId},  //携带的参数           
                method : 'POST',  
                success : function(response, opts) {
                  var list=Ext.decode(response.responseText)||'';
                  for(var i=0;i<Object.keys(list).length;i++){
                  var configItem=[
                        {
                            boxLabel  : list[i].permissionName,
                            name      : 'permissionId',
                            checked:    true,
                            readOnly: true,
                            inputValue: list[i].permissionId,
                            id        : 'checkbox'+i
                        }
                    ];
                if(list[i].permissionType=='页面权限'){
                formItem1.add(configItem[0]);}else{
                  formItem2.add(configItem[0]);
                }
            }
                  },  
                      failure : function(response, opts) { }  
              });
    }});